package view;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SpinnerListModel;
import javax.swing.border.TitledBorder;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;


// TODO: Auto-generated Javadoc
/**
	 * This is the Buya Subcription Details extends AbstractGUI .
	 *
	 * @author Tal Panika
	 */ 
	public class BuyaSubcriptionDetailsGUI extends AbstractGUI
	{
			
		/** The CC vtext field. */
		private JTextField CCVtextField=null;
		
		/** The Credit card numbertext field. */
		private JTextField CreditCardNumbertextField=null;
		
		/** The lbl ccv. */
		private JLabel lblCCV=null;
		
		/** The lbl type. */
		private JLabel lblType=null;
		
		/** The lbl exp date. */
		private JLabel lblExpDate=null;
		
		/** The lbl credit card number. */
		private JLabel lblCreditCardNumber=null;
		
		/** The buy button. */
		private JButton buyButton=null;
		
		/** The content pane. */
		private JPanel contentPane=null;
		
		/** The wrn message. */
		private JLabel wrnMessage=null;
		
		/** The lbl day. */
		private JLabel lblDay=null;
		
		/** The lbl month. */
		private JLabel lblMonth=null;
		
		/** The lbl year. */
		private JLabel lblYear=null;
		
		/** The slash2. */
		private JLabel slash2=null;
		
		/** The slash1. */
		private JLabel slash1=null;
		
		/** The combo box. */
		private  JComboBox comboBox=null;
		
		/** The credit type. */
		private String[] creditType={"American Express","Discover","Master Card","Visa"};
		
		/** The year strings. */
		private String[] yearStrings = {"2014","2015","2016","2017"};
		
		/** The month strings. */
		private String[] monthStrings;
		
		/** The day strings. */
		private String[] dayStrings;	
	    
    	/** The year model. */
    	private SpinnerListModel yearModel;
		
		/** The month model. */
		private SpinnerListModel monthModel;
		
		/** The day model. */
		private SpinnerListModel dayModel;
		
		/** The day spinner. */
		private JSpinner daySpinner;
		
		/** The month spinner. */
		private JSpinner monthSpinner;
		
		/** The year spinner. */
		private JSpinner yearSpinner;
		
		
		
		
		/**
		 * This is BuyaSubcriptionDetailsGUI constructor.
		 * 
		 */
		public BuyaSubcriptionDetailsGUI()
		{
			super();
			
			//Buy photo Or Sample Panel
		    JPanel contentPane=new JPanel();
		    
		    //init spiners
		    yearModel = new SpinnerListModel(yearStrings);
		  
			
			monthStrings = createTimeStrings(12);
			monthModel = new SpinnerListModel(monthStrings);
			
			
			dayStrings = createTimeStrings(31);
			dayModel = new SpinnerListModel(dayStrings);
		
			
		
			
		        contentPane.setOpaque(true);
		        contentPane.setBorder(new TitledBorder(null, "Customer Credit Card Details:", TitledBorder.LEADING, TitledBorder.TOP, null, null));	
		        contentPane.setLayout(null);     
		        
		        contentPane.add(getCCNumberField());	       	           
		        contentPane.add(getCCVField());
		        contentPane.add(getCreditCardNumberLabel());     
		        contentPane.add(getCCVLabel());     
		        contentPane.add(getExpDateLabel());
		        contentPane.add(getBuyButton());
		        contentPane.add(getWrnMessage());
		        contentPane.add(getBackButton());
		    	contentPane.add(getDaySpinner());
		    	contentPane.add(getMonthSpinner());
		    	contentPane.add(getYearSpinner());
		    	contentPane.add(getLblDay());
		    	contentPane.add(getLblMonth());
		    	contentPane.add(getLblYear());
		    	contentPane.add(getSlash2());
		    	contentPane.add(getSlash1());
		    	contentPane.add(getCreditCardTypeLabel());
		    	contentPane.add(getCombo());
		        
		      //Frame
		        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		        this.setTitle("Buy Package Window");
		        this.setContentPane(contentPane); 
		        
		        
		        
		        
		       
		        contentPane.add(comboBox);
			    this.setLocationRelativeTo(null); //This will center the JFrame in the middle of the screen
		        this.setLocationByPlatform(true);
		        this.setSize(415, 338);
		       
		
		}
		
	
		/**
		 * Creates the time strings.
		 *
		 * @param limit the limit
		 * @return the string[]
		 */
		private String[] createTimeStrings(int limit)
		{
			String [] res = new String[limit];
			for (int i = 0; i < limit; i++) 
			{
				res[i] = (i<9 ? "0":"") + (i+1);
			}
			
			return res;
		}
		
		
		/**
		 * Gets the credit card type label.
		 *
		 * @return the credit card type label
		 */
		private JLabel getCreditCardTypeLabel() {
			if (lblType == null) {
				 lblType= new JLabel("Credit Card Type:");
		         lblType.setFont(new Font("Tahoma", Font.PLAIN, 13));
		         lblType.setBounds(41, 186, 113, 20);
			}
			return lblType;
		}
		/**
		 * This method initializes Credit Card Number Label.
		 *
		 * @return the credit card number label
		 */
		private JLabel getCreditCardNumberLabel() {
			if (lblCreditCardNumber == null) {
				    lblCreditCardNumber = new JLabel("Credit Card Number:");
			        lblCreditCardNumber.setBounds(41, 81, 128, 17);
			        lblCreditCardNumber.setFont(new Font("Tahoma", Font.PLAIN, 13));
			}
			return lblCreditCardNumber;
		}
		
		/**
		 * This method initializes CCV Label.
		 *
		 * @return the CCV label
		 */
		private JLabel getCCVLabel() {
			if (lblCCV == null) {
				 lblCCV = new JLabel("CCV:");
			     lblCCV.setBounds(41, 53, 46, 14);
			     lblCCV.setFont(new Font("Tahoma", Font.PLAIN, 13));
			}
			return lblCCV;
		}
		
		/**
		 * This method initializes Expiration Date Label.
		 *
		 * @return the exp date label
		 */
		private JLabel getExpDateLabel() {
			if (lblExpDate == null) {
				 lblExpDate = new JLabel("Expriation Date:");
			        lblExpDate.setBounds(41, 132, 101, 14);
			        lblExpDate.setFont(new Font("Tahoma", Font.PLAIN, 13));
			}
			return lblExpDate;
		}
		
		
		/**
		 * This method initializes Credit Card Number Text Field.
		 *
		 * @return the CC number field
		 */
		private JTextField getCCNumberField() {
			if (CreditCardNumbertextField == null) {
				CreditCardNumbertextField = new JTextField(18);
		        CreditCardNumbertextField.setBounds(168, 78, 128, 20);
		        CreditCardNumbertextField.setColumns(18);
		        CreditCardNumbertextField.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						char c = e.getKeyChar();
						if(((c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_SPACE)))
							e.consume();
					}
				});
		        	      
			}
			return CreditCardNumbertextField;
		}
		
		/**
		 * This method initializes CCV Text Field.
		 *
		 * @return the CCV field
		 */
		private JTextField getCCVField() {
			if (CCVtextField == null) {			
				 CCVtextField = new JTextField(3);
				 CCVtextField.setBounds(168, 51, 54, 20);
				 CCVtextField.setColumns(3);	
				 CCVtextField.addKeyListener(new KeyAdapter() {
						@Override
						public void keyTyped(KeyEvent e) {
							char c = e.getKeyChar();
							if(((c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_SPACE)))
								e.consume();
						}
					});
		       
			}
			return CCVtextField;
		}
		
		

		/**
		 * This method creates warning message if currently is null.
		 * @return JLabel type.
		 */
		private JLabel getWrnMessage() {
			if (wrnMessage == null) {
				wrnMessage = new JLabel();
				wrnMessage.setForeground(Color.RED);
				wrnMessage.setBounds(70, 16, 320, 23);
				wrnMessage.setVisible(false);
			}
			return wrnMessage;
		}
		
		/**
		 * Gets the combo.
		 *
		 * @return the combo
		 */
		private JComboBox getCombo() {	
			if (comboBox == null) {
				
				  comboBox = new JComboBox();
			        comboBox.setBounds(180, 182, 137, 30);
			             
			        comboBox.addItem("Credit Type->");
				
				for(int i=0; i<creditType.length;i++)
					  comboBox.addItem(creditType[i]);
				
				 comboBox.setVisible(true);
			}	
		   return comboBox;
		}
		
		/**
		 * This method initializes Buy Package  Button.
		 *
		 * @return the buy button
		 */
		private JButton getBuyButton() {
			if (buyButton == null) {
				buyButton = new JButton("Buy Packege");
				buyButton.setBounds(55, 240, 113, 30);
				
			}
			return buyButton;
		}
		
		/**
		 * This method initializes Back Button.
		 *
		 * @return the back button
		 */
		private JButton getBackButton() {
			if (backButton == null) {
				backButton = new JButton();
				backButton.setBounds(new Rectangle(250, 245,  113, 30));
				backButton.setText("Back");
			
			}
			return backButton;
		}
		
		
		
		/**
		 * Gets the day spinner.
		 *
		 * @return the day spinner
		 */
		private JSpinner getDaySpinner() {
			if (daySpinner == null) {
				daySpinner = new JSpinner(dayModel);
				daySpinner.setBounds(151, 127, 46, 26);
			}
			return daySpinner;
		}
		
		/**
		 * Gets the month spinner.
		 *
		 * @return the month spinner
		 */
		private JSpinner getMonthSpinner() 
		{
			if (monthSpinner == null) 
			{
				monthSpinner = new JSpinner(monthModel);
				monthSpinner.setBounds(214, 127, 46, 26);
			}
			return monthSpinner;
		}
		
		/**
		 * Gets the year spinner.
		 *
		 * @return the year spinner
		 */
		private JSpinner getYearSpinner() {
			if (yearSpinner == null) {
				yearSpinner = new JSpinner(yearModel);
				yearSpinner.setBounds(283, 127, 54, 26);
			}
			return yearSpinner;
		}
	
		
		/**
		 * Gets the lbl day.
		 *
		 * @return the lbl day
		 */
		private JLabel getLblDay() {
			if (lblDay == null) {
				 lblDay = new JLabel("day");
				 lblDay.setBounds(155, 107, 42, 20);
			}
			return lblDay;
		}
		
		
		/**
		 * Gets the lbl month.
		 *
		 * @return the lbl month
		 */
		private JLabel getLblMonth() {
			if (lblMonth == null) {
			    lblMonth = new JLabel("month");
				lblMonth.setBounds(214, 107, 54, 20);
			}
			return lblMonth;
		}
		
		/**
		 * Gets the lbl year.
		 *
		 * @return the lbl year
		 */
		private JLabel getLblYear() {
			if (lblYear == null) {
				lblYear = new JLabel("year");
				lblYear.setBounds(290, 107, 30, 20);
			}
			return lblYear;
		}
		
		
		/**
		 * Gets the slash2.
		 *
		 * @return the slash2
		 */
		private JLabel getSlash2() {
			if (slash2 == null) {
				slash2 = new JLabel("/");
				slash2.setBounds(267, 130, 6, 20);
			}
			return slash2;
		}
		
		/**
		 * Gets the slash1.
		 *
		 * @return the slash1
		 */
		private JLabel getSlash1() {
			if (slash1 == null) {
				slash1 = new JLabel("/");
				slash1.setBounds(203, 130, 6, 20);
			}
			return slash1;
		}
		
		
		
		
		/**
		 * ******************************************* public method.
		 *
		 * @param msg the msg
		 */
			/**
			 * This method display Warning Message.
			 * @param void	 
			 * */
			
			public void displayWrnMessage(String msg){
				wrnMessage.setText(msg);
				wrnMessage.setVisible(true);
			}
			
			/**
			 * This method add action listener to Buy button.
			 *
			 * @param listener the listener
			 */
			public void addBuyActionListener(ActionListener listener){
				buyButton.addActionListener(listener);
			}
			
			
			/**
			 * This method return -simulate back button pressed .
			 */
			public void clickBackButton()
			{
				//backButton=null;
				this.getBackButton().doClick();
			}
			
			
			/**
			 * This method returns Credit Card Number Text Field.
			 *
			 * @return the CC number
			 */
			public String getCCNumber() {
				
				return CreditCardNumbertextField.getText();
			}
			
			/**
			 * This method return CCV Text Field.
			 *
			 * @return the ccv
			 */
			public String getCCV() {
			
				return CCVtextField.getText();
			}
			
			
			
			/**
			 * This method returns the selected Date in "yyyy-MM-dd" format.
			 *
			 * @return Date string
			 */
			public String getDateFormtDB()
			{
				String yyyy = (String) getYearSpinner().getValue();
				String MM = (String) getMonthSpinner().getValue();
				String dd = (String) getDaySpinner().getValue();
				
				
				// set the Date format: "yyyy-MM-dd "
				String Date = yyyy+"-"+MM+"-"+dd;
				return Date;
			}
			
			/**
			 * This method returns the selected Date in "dd/MM/yyyy" format.
			 *
			 * @return Date string
			 */
			public String getDateFormtValidate()
			{
				String yyyy = (String) getYearSpinner().getValue();
				String MM = (String) getMonthSpinner().getValue();
				String dd = (String) getDaySpinner().getValue();
				
				
				// set the Date format: "yyyy-MM-dd "
				String Date = dd+"/"+MM+"/"+yyyy;
				return Date;
			}
			
			/**
			 * Gets the credit type index.
			 *
			 * @return the credit type index
			 */
			public int getCreditTypeIndex()
			{
	             int index;
				
				index=this.getCombo().getSelectedIndex();
				
				return index;
			}
			
			/**
			 * Gets the credit type.
			 *
			 * @return the credit type
			 */
			public String getCreditType()
			{
				int index;
				
				index=this.getCombo().getSelectedIndex();
								
						return creditType[index-1];
					
			}
			
			/**
			 * Clear fields.
			 */
			public void clearFields(){
				this.getCCNumberField().setText("");
				this.getCCVField().setText("");
				
			}
}
